package com.qyhl.webtv.module_live.teletext.list;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gyf.immersionbar.ImmersionBar;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.constant.LiveConstant;
import com.qyhl.webtv.commonlib.entity.live.LiveRoomBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.analytics.MobclickAgent;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 直播列表
 *
 * @author helin
 */
@Route(path = ARouterPathConstant.LIVELIST)
public class LiveListActivity extends BaseActivity implements LivesListContract.LiveListView {

    @BindView(R2.id.title)
    TextView mTitle;
    @BindView(R2.id.live_recyclerView)
    ListView mLiveRecyclerView;
    @BindView(R2.id.live_refresh)
    SmartRefreshLayout mLliveRefresh;
    @BindView(R2.id.lives_loading)
    LoadingLayout mLoading;
    @BindView(R2.id.title_left)
    ImageView titleLeft;
    @BindView(R2.id.title_right)
    ImageView titleRight;
    private LivesListPresenter mPresenter;
    List<LiveRoomBean> mData = new ArrayList<>();
    private CommonAdapter<LiveRoomBean> mAdatapter;

    @Autowired(name = "title")
    String title;

    private String liveId = "0";
    private String liveType = "0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.live_activity_live_list;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        mPresenter = new LivesListPresenter(this);
        inits();
    }

    @Override
    protected void setListener() {

    }

    private void inits() {
        titleLeft.setVisibility(View.VISIBLE);
        mTitle.setText(StringUtils.isEmpty(title) ? "直播列表" : title);
        mLliveRefresh.setEnableLoadMore(false);
        //刷新中禁止操作
        mLliveRefresh.setDisableContentWhenRefresh(true);
        mLliveRefresh.setRefreshHeader(new MaterialHeader(this));
        mLliveRefresh.setRefreshFooter(new ClassicsFooter(this));
        mLoading.setStatus(LoadingLayout.Loading);
        mLiveRecyclerView.setAdapter(mAdatapter = new CommonAdapter<LiveRoomBean>(this, R.layout.live_item_lives_list, mData) {
            @Override
            protected void convert(ViewHolder holder, LiveRoomBean liveRoomBean, int position) {
                holder.setText(R.id.item_live_title, liveRoomBean.getTitle());
                String hitCount = liveRoomBean.getHitcount();
                if (!StringUtils.isEmpty(hitCount)) {
                    holder.setText(R.id.item_live_friends, hitCount + "人参与");
                } else {
                    holder.setText(R.id.item_live_friends, "0人参与");
                }

                ImageView bg = holder.getView(R.id.item_live_logo);
                ImageView tag = holder.getView(R.id.item_live_tagtype);
                ImageView review = holder.getView(R.id.live_status);
                Glide.with(LiveListActivity.this).load(liveRoomBean.getLogo()).apply(new RequestOptions().error(R.drawable.icon_live_no_img)).into(bg);

                review.setVisibility(View.GONE);
                String type = liveRoomBean.getLivetype();
                if ((LiveConstant.MEDIA.equals(type))) {
                    //电视直播
                    tag.setImageResource(R.drawable.icon_lives_tv);
                } else if (LiveConstant.CAMERA.equals(type)) {
                    //移动直播
                } else if (LiveConstant.INTERACTIVE.equals(type)) {
                    //互动直播
                    tag.setImageResource(R.drawable.icon_lives_hulive);
                } else if (LiveConstant.AUDIO.equals(type)) {
                    tag.setImageResource(R.drawable.icon_lives_tv);
                } else if (LiveConstant.PROGRAM.equals(type)) {
                    if ("1".equals(liveRoomBean.getType())) {
                        tag.setImageResource(R.drawable.icon_lives_tv);
                    } else {
                        tag.setImageResource(R.drawable.icon_lives_radio);
                    }
                } else if (LiveConstant.LIVENEWS.equals(type)) {
                    //图文直播
                    tag.setImageResource(R.drawable.icon_lives_media);
                    //状态
                    int status = Integer.parseInt(liveRoomBean.getStatus());
                    switch (status) {
                        case 6:
                            //预告
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_live);
                            break;
                        case 7:
                            //回放
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_review);
                            break;
                        case 2:
                            //直播中
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_live);
                            break;
                        case 3:
                            //结束
                            review.setVisibility(View.VISIBLE);
                            review.setImageResource(R.drawable.icon_lives_end);
                            break;
                        default:
                            break;
                    }
                } else {
                    tag.setImageResource(R.drawable.icon_lives_no);
                }
            }


        });

        titleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mLliveRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                liveId = "0";
                liveType = "0";
                mPresenter.getLiveList(liveId, liveType);
            }
        });

        mLoading.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mLoading.setReloadButtonText("加载中...");
                liveId = "0";
                liveType = "0";
                mPresenter.getLiveList(liveId, liveType);
            }
        });

        mLliveRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getLiveList(liveId, liveType);
            }
        });

        //item 点击事件
        mLiveRecyclerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LiveRoomBean liveBean = mData.get(position);
                String type = liveBean.getLivetype();
                if ((LiveConstant.MEDIA.equals(type))) {
                    //电视直播
                    Bundle bundle = new Bundle();
                    bundle.putString("id", liveBean.getId());
                    RouterManager.router(ARouterPathConstant.LIVE_VIDEO,bundle);
                } else if (LiveConstant.CAMERA.equals(type)) {
                    //移动直播
                } else if (LiveConstant.INTERACTIVE.equals(type)) {
                    //互动直播
                } else if (LiveConstant.AUDIO.equals(type)) {
                    Bundle bundle = new Bundle();
                    bundle.putString("id", liveBean.getId());
                    RouterManager.router(ARouterPathConstant.LIVE_RADIO,bundle);
                    //电台直播
                } else if (LiveConstant.LIVENEWS.equals(type)) {
                    //图文直播
                    //预告
                    Bundle bundle = new Bundle();
                    bundle.putString("id", liveBean.getRoomId()+"");
                    RouterManager.router(ARouterPathConstant.TELE_VIDEO_ACTIVITY,bundle);
                } else if (LiveConstant.PROGRAM.equals(type)) {
                    if ("1".equals(liveBean.getType())) {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", liveBean.getRoomId()+"");
                        RouterManager.router(ARouterPathConstant.LIVE_VIDEO,bundle);
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("id", liveBean.getRoomId()+"");
                        RouterManager.router(ARouterPathConstant.LIVE_RADIO,bundle);
                    }
                } else {

                }

            }
        });

        mPresenter.getLiveList(liveId, liveType);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R2.id.title_right, R2.id.title_left})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.title_right) {
        } else if (i == R.id.title_left) {
            finish();

        } else {
        }
    }

    /**
     * 获取数据失败
     *
     * @param message
     */
    @Override
    public void error(String message, boolean isMore) {
        if (isMore) {
            mLliveRefresh.finishLoadMore();
            showToast(message, 4);
        } else {
            mLliveRefresh.finishRefresh();
            errorLoad(message, mLoading);
        }

    }

    /**
     * 获取成功
     *
     * @param response
     */
    @Override
    public void success(List<LiveRoomBean> response, boolean isMore) {
        mLoading.setStatus(LoadingLayout.Success);
        if (isMore) {
            mLliveRefresh.finishLoadMore();
        } else {
            mLliveRefresh.finishRefresh();
            mData.clear();
        }
        liveId = response.get(response.size() - 1).getRoomId() + "";
        liveType = response.get(response.size() - 1).getLivetype();
        mData.addAll(response);
        mAdatapter.notifyDataSetChanged();

    }

    /**
     * 空数据
     */
    @Override
    public void empty(boolean isMore) {
        if (isMore) {
            mLliveRefresh.finishLoadMore();
        } else {
            mLliveRefresh.finishRefresh();
            emptyLoad(mLoading);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        //baidu
        //umeng
        MobclickAgent.onPageStart("直播列表");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //baidu
        //umeng
        MobclickAgent.onPageEnd("直播列表");
        MobclickAgent.onPause(this);
    }
}
